﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using Newtonsoft.Json;
using ZHDJ.Core.Util;
using ZHDJ.Data.BLL;
using ZHDJ.Data.Models.Org;
using ZHDJ.WebPage;
using ZHDJ.Data.Models.Common;
using ZHDJ.Core.Log;
using System.Data;
using MySql.Data.MySqlClient;
using ZHDJ.Web.CommonMethod;
using ZHDJ.Core;
using ZHDJ.Data.Models.Edu;

namespace ZHDJ.Web
{
    public partial class BigFileUpload : PortalPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ControlBusiness.ControlBindEx(this.drpCourseType, DB.DataDictionarys.Where(o => o.DDicType == SysDataDictionary.EduThemeCategory).ToList(), "DDicMemberName", "DDicMember");
            ControlBusiness.ControlBindEx(this.drpCourseFormat, DB.DataDictionarys.Where(o => o.DDicType == SysDataDictionary.EduCourseType).ToList(), "DDicMemberName", "DDicMember");
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (AttachFile.HasFile)
            {
                //保存文件
                string FileName = this.AttachFile.FileName;//获取上传文件的文件名,包括后缀
                string ExtenName = System.IO.Path.GetExtension(FileName);//获取扩展名
                string SaveFileName = System.IO.Path.Combine(
                    System.Web.HttpContext.Current.Request.MapPath("UpLoad/BigFile/"),
                     DateTime.Now.ToString("yyyyMMddhhmm") + ExtenName);//合并两个路径为上传到服务器上的全路径
                AttachFile.MoveTo(SaveFileName, Brettle.Web.NeatUpload.MoveToOptions.Overwrite);

                string url = "UpLoad/BigFile/" + DateTime.Now.ToString("yyyyMMddhhmmss") + ExtenName;  //文件保存的路径
                float FileSize = (float)System.Math.Round((float)AttachFile.ContentLength / 1024000, 1); //获取文件大小并保留小数点后一位,单位是M

                //插入文件表
                var userid = "";
                if (CurrentUser == null)
                {
                    userid = "admin";
                }
                else
                {
                    userid = CurrentUser.ID;
                }

                PartyFile partyFile = new PartyFile();
                partyFile.CreateBy = userid;
                partyFile.CreateTime = DateTime.Now;
                partyFile.Description = "PartyEduCourseware-File";
                partyFile.Url = url;
                partyFile.Size = (int)FileSize;
                partyFile.Title = FileName;
                partyFile.ID = StringHelper.NewID;
                partyFile.Extension = ExtenName;

                DB.PartyFiles.Add(partyFile);
                DB.SaveChanges();

                lblfileurl.Text = url;
                uploadfileid.Value = partyFile.ID;
            }
        }

        protected void sure_Click(object sender, EventArgs e)
        {
            //插入课件表
            var userid = "";
            if (CurrentUser == null)
            {
                userid = "admin";
            }
            else
            {
                userid = CurrentUser.ID;
            }
            var partyEduCourseware = new PartyEduCourseware();
            partyEduCourseware.ID = StringHelper.NewID;
            partyEduCourseware.CreateTime = DateTime.Now;
            partyEduCourseware.CreateBy = userid;
            partyEduCourseware.ModifyTime = DateTime.Now;
            partyEduCourseware.ModifyBy = userid;
            partyEduCourseware.Module = "PartyEduCourseware";
            partyEduCourseware.Title = txtCourseName.Text;
            partyEduCourseware.Duration = "";
            partyEduCourseware.Summary = "";
            partyEduCourseware.TargetObject = "";
            partyEduCourseware.Category = drpCourseType.SelectedValue;
            partyEduCourseware.CourseType = drpCourseFormat.SelectedValue;
            partyEduCourseware.Flags = 0;
            partyEduCourseware.Status = 4;
            partyEduCourseware.FileID = uploadfileid.Value;
            DB.PartyEduCoursewares.Add(partyEduCourseware);
            DB.SaveChanges();

            clearData();
        }

        private void clearData()
        {
            txtCourseName.Text = "";
            lblfileurl.Text = "";
            uploadfileid.Value = "";

        }
    }
}